package com.jin_mo.custom.adapter.find_id;


import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.moudle.custom.R;

import java.util.LinkedHashSet;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class RVBaseViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;
    private View mItemView;
    private RVBaseAdapter adapter;


    public RVBaseViewHolder(View itemView) {
        super(itemView);
        views = new SparseArray<>();
        mItemView = itemView;
    }

    protected RVBaseViewHolder setAdapter(RVBaseAdapter adapter) {
        this.adapter = adapter;
        return this;
    }

    /**
     * 获取ItemView
     *
     * @return
     */
    public View getItemView() {
        return mItemView;
    }

    private TextView getTextView(int resId) {
        return retrieveView(resId);
    }

    private ImageView getImageView(int resId) {
        return retrieveView(resId);
    }

    /**
     * 通过id得到view
     */
    public View getView(int resId) {
        return retrieveView(resId);
    }

    @SuppressWarnings("unchecked")
    private <V extends View> V retrieveView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = mItemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (V) view;
    }


    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 背景设置  ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    public RVBaseViewHolder setBackGround(@IdRes int viewId, @DrawableRes int resId) {
        getView(viewId).setBackgroundResource(resId);
        return this;
    }


    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 背景色设置   ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    public RVBaseViewHolder setBackColor(@IdRes int viewId, @ColorInt int color) {
        getView(viewId).setBackgroundColor(color);
        return this;
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 权重设置   ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    public RVBaseViewHolder setElevation(@IdRes int viewId, int elevation) {
        getView(viewId).setElevation(elevation);
        return this;
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 TextView 加载  ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /**
     * 设置TextView文本
     */
    public RVBaseViewHolder setText(@IdRes int viewId, CharSequence value) {
        getTextView(viewId).setText(value);
        return this;
    }

    public RVBaseViewHolder setText(@IdRes int viewId, int value) {
        getTextView(viewId).setText(String.valueOf(value));
        return this;
    }

    /**
     * 设置Textview字体颜色
     */

    public RVBaseViewHolder setTextColor(@IdRes int viewId, @ColorInt int textColor) {
        getTextView(viewId).setTextColor(textColor);
        return this;
    }

    public RVBaseViewHolder setTextColor(@IdRes int viewId, ColorStateList colors) {
        getTextView(viewId).setTextColor(colors);
        return this;
    }

    public RVBaseViewHolder setTextColor(@IdRes int viewId, String color) {
        getTextView(viewId).setTextColor(Color.parseColor(color));
        return this;
    }

    /**
     * 设置Textview字体加粗
     */
    public RVBaseViewHolder seFakeBoldText(@IdRes int viewId, boolean fakeBoldText) {
        getTextView(viewId).getPaint().setFakeBoldText(fakeBoldText);
        return this;
    }

    /**
     * 设置TextView字体大小
     * COMPLEX_UNIT_SP ---sp
     * COMPLEX_UNIT_PX ---px
     * COMPLEX_UNIT_DIP ---dp
     */

    public RVBaseViewHolder setSpTextSize(@IdRes int viewId, float spSize) {
        getTextView(viewId).setTextSize(TypedValue.COMPLEX_UNIT_SP, spSize);
        return this;
    }

    public RVBaseViewHolder setPxTextSize(@IdRes int viewId, float pxSize) {
        getTextView(viewId).setTextSize(TypedValue.COMPLEX_UNIT_PX, pxSize);
        return this;
    }

    public RVBaseViewHolder setDpTextSize(@IdRes int viewId, float dpSize) {
        getTextView(viewId).setTextSize(TypedValue.COMPLEX_UNIT_DIP, dpSize);
        return this;
    }

    /**
     * 设置TextView字体
     */
    public RVBaseViewHolder setTextFont(@IdRes int viewId, String fontPath) {
        Typeface typeface = Typeface.createFromAsset(getTextView(viewId).getContext().getAssets(), fontPath);
        getTextView(viewId).setTypeface(typeface);
        return this;
    }

    /**
     * 获取TextView文本
     */
    public CharSequence getText(@IdRes int viewId) {
        return getText((TextView) getView(viewId));
    }

    /**
     * 获取Textview文本
     */
    private CharSequence getText(TextView view) {
        if (view != null) {
            return view.getText();
        }
        return "";
    }


    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 ImageView 加载  ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    public RVBaseViewHolder setImageFromSelect(@IdRes int viewId, boolean isSelect) {
        getImageView(viewId).setSelected(isSelect);
        return this;
    }

    /**
     * 加载图片
     *
     * @param viewId
     * @return Glide加载
     */
    public RVBaseViewHolder setImageFromGlide(@IdRes int viewId, String imgUrl, int loading, int error) {
        Glide.with((getImageView(viewId)).getContext()).load(imgUrl).placeholder(loading).error(error).fitCenter().into(getImageView(viewId));
        return this;
    }

    public RVBaseViewHolder setImageFromGlide(@IdRes int viewId, String imgUrl) {
        Glide.with((getImageView(viewId)).getContext()).load(imgUrl).placeholder(R.mipmap.ic_load).error(R.mipmap.ic_error).fitCenter().into(getImageView(viewId));
        return this;
    }

    public RVBaseViewHolder setImageFromGlide(@IdRes int viewId, String imgUrl, ViewGroup.LayoutParams layoutParams) {
        Glide.with((getImageView(viewId)).getContext()).load(imgUrl)
                .placeholder(R.mipmap.ic_load)
                .error(R.mipmap.ic_error)
                .override(layoutParams.width, Integer.MAX_VALUE)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
                .into(getImageView(viewId));
        return this;
    }

    /**
     * 高斯模糊
     * @param fuzzy 模糊程度
     * @return
     */
    public RVBaseViewHolder setImageFromGlide(@IdRes int viewId, int imageId, int fuzzy) {
        Glide.with((getImageView(viewId)).getContext())
                .load(imageId)
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(fuzzy,3)))
                .into(getImageView(viewId));

        return this;
    }

    /**
     * 加载图片
     *
     * @param viewId
     * @return ImageView加载
     */

    public RVBaseViewHolder setImageFromImage(@IdRes int viewId, int imgUrl) {
        getImageView(viewId).setImageResource(imgUrl);
        return this;
    }

    public RVBaseViewHolder setImageRound(@IdRes int viewId, int imgUrl) {
        Glide.with((getImageView(viewId)).getContext()).load(imgUrl).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(getImageView(viewId));
        return this;
    }

    public RVBaseViewHolder setImageFromImage(@IdRes int viewId, String imgUrl) {
        Uri uri = Uri.parse(imgUrl);
        getImageView(viewId).setImageURI(uri);
        return this;
    }

    public RVBaseViewHolder setImageFromImage(@IdRes int viewId, Bitmap imgUrl) {
        getImageView(viewId).setImageBitmap(imgUrl);
        return this;
    }

    public RVBaseViewHolder setImageFromImage(@IdRes int viewId, Drawable imgUrl) {
        getImageView(viewId).setImageDrawable(imgUrl);
        return this;
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 控件的显示隐私  ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    public RVBaseViewHolder setVisibility(@IdRes int viewId, int visibility) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visibility);
        }
        return this;
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  以下是关于 控件的点击事件  ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /**
     * setOnItemClickListener --单次点击事件
     * setOnLongClickListener --长按事件
     * setOnTouchClickListener --触摸事件
     */

    public RVBaseViewHolder setOnItemClickListener(@IdRes int viewId, View.OnClickListener onClickListener) {
        getView(viewId).setOnClickListener(onClickListener);
        return this;
    }

    public RVBaseViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener onClickListener) {
        getView(viewId).setOnLongClickListener(onClickListener);
        return this;
    }

    public RVBaseViewHolder setOnTouchClickListener(int viewId, View.OnTouchListener onClickListener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnTouchListener(onClickListener);
        }
        return this;
    }


    /**
     * 用于保存需要设置点击事件的 item
     */
    private final LinkedHashSet<Integer> childClickViewIds = new LinkedHashSet<>();

    //长按事件
    private final LinkedHashSet<Integer> childLongClickViewIds = new LinkedHashSet<>();


    public RVBaseViewHolder addOnClickListener(@IdRes final int... viewIds) {
        for (int viewId : viewIds) {
            childClickViewIds.add(viewId);
            final View view = getView(viewId);
            if (view != null) {
                if (!view.isClickable()) {
                    view.setClickable(true);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (adapter.getOnItemChildClickListener() != null) {
                            int position = getAdapterPosition();
                            if (position == RecyclerView.NO_POSITION) {
                                LogUtils.e(RecyclerView.NO_POSITION);
                                return;
                            }
                            if (adapter.isShowEmptyView()) {
                                adapter.getOnItemChildClickListener().onItemClick(adapter, v, null, 0);
                            } else {
                                adapter.getOnItemChildClickListener().onItemClick(adapter, v, adapter.mList.get(position), position);
                            }
                        }
                    }
                });
            }
        }
        return this;
    }

    public RVBaseViewHolder addOnLongClickListener(@IdRes final int... viewIds) {
        for (int viewId : viewIds) {
            childLongClickViewIds.add(viewId);
            final View view = getView(viewId);
            if (view != null) {
                if (!view.isClickable()) {
                    view.setClickable(true);
                }
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (adapter.getOnItemLongChildClickListener() != null) {
                            int position = getAdapterPosition();
                            if (position == RecyclerView.NO_POSITION) {
                                LogUtils.e(RecyclerView.NO_POSITION);
                                return false;
                            }
                            if (adapter.isShowEmptyView()) {
                                adapter.getOnItemLongChildClickListener().onItemLongClick(adapter, v, null, 0);
                            } else {
                                adapter.getOnItemLongChildClickListener().onItemLongClick(adapter, v, adapter.mList.get(position), position);
                            }
                        }
                        return false;
                    }
                });
            }
        }
        return this;
    }

    public LinkedHashSet<Integer> getChildClickViewIds() {
        return childClickViewIds;
    }

    public LinkedHashSet<Integer> getChildLongClickViewIds() {
        return childLongClickViewIds;
    }

}
